package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Business;
import com.wn.class30.service.BusinessService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.StringToArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/houtai/page/deploy/shopdeploy/")
public class BusinessController {
    @Resource
    private BusinessService businessService;

    @RequiresPermissions("/houtai/page/deploy/shopdeploy/list")
    @RequestMapping("list")
    public Result selectBusiness(Integer page,Integer limit)throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf((page-1)*limit));
        map.put("limit",String.valueOf(limit));
        Long orderCount = businessService.selectBusinessCount(map);
        Page p = new Page();
        p.setCurrentPage((page-1)*limit);
        p.setTotalSize(orderCount);
        p.setPageSize(limit);
        long pageSize = orderCount % limit == 0? orderCount / limit:orderCount / limit + 1;
        p.setPageSize((int)pageSize);
        List<Business> businesses = businessService.selectBusiness(map);
        return new Result("0","success",p,businesses);
    }

    @RequiresPermissions("/houtai/page/deploy/shopdeploy/insert")
    @RequestMapping("insert")
    public Result insertBusiness(Business business)throws Exception{
        if(business != null){
            businessService.insertBusiness(business);
            return new Result("200","success",null,null);
        }
        return new Result("200","success",null,null);
    }

    @RequiresPermissions("/houtai/page/deploy/shopdeploy/update")
    @RequestMapping("update")
    public Result updateBusinessById(Business business)throws Exception{
        if(business != null){
            businessService.updateBusinessById(business);
            return new Result("200","success",null,null);
        }
        return new Result("200","success",null,null);
    }

    @RequiresPermissions("/houtai/page/deploy/shopdeploy/delete")
    @RequestMapping("delete")
    public Result deleteBusinessById(String businessIds)throws Exception{
        if(businessIds != null && !"".equals(businessIds.trim())){
            Integer[] array = StringToArray.getArray(businessIds);
            for (Integer integer : array) {
                businessService.deleteBusinessById(integer);
            }
            return new Result("200","success",null,null);
        }
        return new Result("200","success",null,null);
    }


}
